<?
$starta = microtime_float();

$min_year = getMinYear($GLOBALS['id']);
if(!is_numeric($min_year)){
	$min_year = date('Y');
}

$num_years = date('Y') - $min_year + 1;

$general_result = getGeneralResults();

//print_r(getGeneralResults(array(), array(), 'query'));

$stores = getStores($GLOBALS['id']);

if($general_result['result']){

	// get table data
	$curr_inv_res = getByCurr('inv');
	$curr_cn_res = getByCurr('cn');
	$monthly_inv_res = getByMoth('inv');
	$monthly_cn_res = getByMoth('cn');
	$yearly_inv_res = getByYear('inv');
	$yearly_cn_res = getByYear('cn');

	$res_table1 = "<table class='bTable' cellspacing=0 cellpadding=0 width='100%'>
	<tr>
		<th>Date</th>
		<th>Order ID</th>
		<th>Type</th>
		<th>Retail</th>
		<th>Wholesale</th>
		<th>Client</th>
		<th>Username</th>
	</tr>";

	// build tables
	foreach($general_result['result'] as $k => $v){
		$date = date('m/d/Y', strtotime($v['sale_date']));
		$retail = $GLOBALS['currencies'][($v['payment_currency'])].' '.$v['sum_amount_total'];
		$wholesale = $GLOBALS['currencies'][($v['payment_currency'])].' '.$v['sum_amount_wholesale'];
		if($v['sale_type'] != 'reversal'){
			$type = 'Invoice';
		}else{
			$type = 'Credit Note';
			$retail = "<font color='red'>$retail</font>";
			$wholesale = "<font color='red'>$wholesale</font>";
		}

		$client = $v['client_name'];
		$res_table1 .= "<tr>
			<td>$date</td>
			<td><a href='#' onClick=\"openPage('account(billing).html', 'info[order_id]=$v[order_id]&info[client_id]=$v[client_id]' + '&return_to=' + encodeURI('openPage(\'general.html\', \'stepPage=general1.html\');')); return false;\">$v[order_id]</a></td>
			<td>$type</td>
			<td align='right' style='padding-right:10px;'>$retail</td>
			<td align='right' style='padding-right:10px;'>$wholesale</td>
			<td>$client</td>
			<td><a href='#' onClick=\"openPage('account(billing).html', 'info[client_id]=$v[client_id]' + '&return_to=' + encodeURI('openPage(\'general.html\', \'stepPage=general1.html\');')); return false;\">$v[client_username]</a></td>
		</tr>";
	}
	$res_table1 .= '</table>';

	$res_table2 = "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
	$res_table2 .= "<tr style='background-color:#DCDCDC;'>
		<td><strong>By Month</strong></td>";

	for($i = 0; $i<6; $i++){
		$year_month = date("Y-m", mktime(0, 0, 0, date('m') - $i, 1, date('Y')));
		$year = date("Y", mktime(0, 0, 0, date('m') - $i, 1, date('Y')));
		$month = date("m", mktime(0, 0, 0, date('m') - $i, 1, date('Y')));
		$res_table2 .= "<td><strong><a href='#' onClick=\"document.getElementById('years').value='$year';document.getElementById('months').value='$month';sgeneral(getFormData('generalSearch'));\">".date("M'y", mktime(0, 0, 0, date('m') - $i, 1, date('Y')))."</a></strong></td>";
	}
	$res_table2 .= "</tr>";

	$res_table2 .= "<tr>
		<td>Invoices</td>";
	for($i = 0; $i<6; $i++){
		$year_month = date("Y-m", mktime(0, 0, 0, date('m') - $i, 1, date('Y')));
		$inv = $monthly_inv_res[$year_month]['count'] ? $monthly_inv_res[$year_month]['count'] : 0;
		$cn = $monthly_cn_res[$year_month]['count'] ? $monthly_cn_res[$year_month]['count'] : 0;
		$all = $inv + $cn;
		$res_table2 .= "<td>$inv / $all</td>";
	}
	$res_table2 .= "</tr>";

	$res_table2 .= "<tr>
		<td>Credit Notes</td>";
	for($i = 0; $i<6; $i++){
		$year_month = date("Y-m", mktime(0, 0, 0, date('m') - $i, 1, date('Y')));
		$inv = $monthly_inv_res[$year_month]['count'] ? $monthly_inv_res[$year_month]['count'] : 0;
		$cn = $monthly_cn_res[$year_month]['count'] ? $monthly_cn_res[$year_month]['count'] : 0;
		$all = $inv + $cn;
		$res_table2 .= "<td>$cn / $all</td>";
	}
  	$res_table2 .= "</tr>";
	$res_table2 .= '</table>';

	$res_table3 = "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
	$res_table3 .= "<tr style='background-color:#DCDCDC;'>
		<td><strong>By Year</strong></td>";

	for($i = 0; $i < 6; $i++){
		if($num_years > $i){
			$year = date("Y", mktime(0, 0, 0, date('m'), 1, date('Y') - $i));
			$res_table3 .= "<td><strong><a href='#' onClick=\"document.getElementById('years').value='$year';sgeneral(getFormData('generalSearch'));\">$year</a></strong></td>";
		}else{
			$res_table3 .= "<td>&nbsp&nbsp&nbsp</td>";
		}
	}
	$res_table3 .= "</tr>";
	$res_table3 .= "<tr>
		<td>Invoices</td>";
	for($i = 0; $i < 6; $i++){
		$year = date("Y", mktime(0, 0, 0, date('m'), 1, date('Y') - $i));
		$inv = $yearly_inv_res[$year]['count'] ? $yearly_inv_res[$year]['count'] : 0;
		$cn = $yearly_cn_res[$year]['count'] ? $yearly_cn_res[$year]['count'] : 0;
		$all = $inv + $cn;
		if($num_years > $i){
			$res_table3 .= "<td>$inv / $all</td>";
		}else{
			$res_table3 .= "<td>&nbsp&nbsp&nbsp</td>";
		}
	}
	$res_table3 .= "</tr>";

	$res_table3 .= "<tr>
		<td>Credit Notes</td>";
	for($i = 0; $i < 6; $i++){
		$year = date("Y", mktime(0, 0, 0, date('m'), 1, date('Y') - $i));
		$inv = $yearly_inv_res[$year]['count'] ? $yearly_inv_res[$year]['count'] : 0;
		$cn = $yearly_cn_res[$year]['count'] ? $yearly_cn_res[$year]['count'] : 0;
		$all = $inv + $cn;
		if($num_years > $i){
			$res_table3 .= "<td>$cn / $all</td>";
		}else{
			$res_table3 .= "<td>&nbsp&nbsp&nbsp</td>";
		}
	}
  	$res_table3 .= "</tr>";
	$res_table3 .= '</table>';

	$res_table4 = "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
	$res_table4 .= "<tr style='background-color:#DCDCDC;'>
		<td><strong>By Currency</strong></td>";

	foreach($GLOBALS['currencies'] as $k => $v){
		$res_table4 .= "<td><strong><a href='#' onClick=\"document.getElementById('currencies').value = '$k';sgeneral(getFormData('generalSearch'));\">$k</a></strong></td>";
	}

	$res_table4 .= "</tr>";
	$res_table4 .= "<tr>
		<td>Invoices</td>";

	foreach($GLOBALS['currencies'] as $k => $v){
		$inv = $curr_inv_res[$k]['count'] ? $curr_inv_res[$k]['count'] : 0;
		$cn = $curr_cn_res[$k]['count'] ? $curr_cn_res[$k]['count'] : 0;
		$all = $inv + $cn;
		$res_table4 .= "<td>$inv / $all</td>";
	}

	$res_table4 .= "</tr>";
	$res_table4 .= "<tr>
		<td>Credit Notes</td>";
	foreach($GLOBALS['currencies'] as $k => $v){
		$inv = $curr_inv_res[$k]['count'] ? $curr_inv_res[$k]['count'] : 0;
		$cn = $curr_cn_res[$k]['count'] ? $curr_cn_res[$k]['count'] : 0;
		$all = $inv + $cn;
		$res_table4 .= "<td>$cn / $all</td>";
	}

  	$res_table4 .= "</tr>";
	$res_table4 .= '</table>';

}

$enda = microtime_float();
//echo "<br>Executed in ".($enda - $starta)." seconds<br>";

?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
	<td align="left">
	  <p><strong>A View from a Different Angle</strong><br>
	  Based on the prices of the services offered by you, the Invoices/Credit Notes are marked as either Retail or Wholesale.<br>
	  Each Retail Invoice or Credit Note always goes hand in hand with a Wholesale one.<br>
	  If the original payment is based on a Wholesale price, i.e. if you have made a sale through ResellersPanel's Control Panel or the API, then ONLY a Wholesale Invoice is being issued, while a Wholesale Credit Note is being issued upon partial or full refund of the Invoice. <br>
	A Self-Billing Tax Invoice is being issued only upon successful receipt of your commissions. For more information, please read the detailed '<a href="#">Help</a>' section.</p></td>
	<td align="right"></td>
  </tr>
</table>
<form name="generalSearch" id="generalSearch" onSubmit="return false;">
<table class="reg_table" width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
	<td>Month:<br />
	  <select name="info[month]" id='months'>
		<option selected value="">All Months</option>
		<option value="01">January</option>
		<option value="02">February</option>
		<option value="03">March</option>
		<option value="04">April</option>
		<option value="05">May</option>
		<option value="06">June</option>
		<option value="07">July</option>
		<option value="08">August</option>
		<option value="09">September</option>
		<option value="10">October</option>
		<option value="11">November</option>
		<option value="12">December</option>
	  </select></td>
	<td>Year:<br />
	  <select name="info[year]" id='years'>
		<option selected value="">Any</option>
		<?
		for($i = 0; $i < $num_years; $i++){
			$year = date("Y", mktime(0, 0, 0, date('m'), 1, date('Y') - $i));
			echo "<option value='$year'>$year</option>";
		}
		?>
	  </select></td>
	<td>Currency:<br />
	  <select name="info[payment_currency]" id='currencies'>
		<option selected value="">All</option>
		<?
		foreach ($GLOBALS['currencies'] as $k => $v){
			echo "<option value='$k'>$k</option>";
		}
		?>
	  </select></td>
	<td>Type:<br />
	  <select name="info[type]">
		<option selected value="">All Types</option>
		<option value="invoice">Invoice</option>
		<option value="credit_note">Credit Note</option>
	  </select></td>
	<td>Store:<br />
	  <select name="info[store_id]">
		<option selected value="">All Stores</option>
		<?
		foreach($stores as $k => $v){
			echo "<option value='$v[store_id]'>$v[title]</option>";
		}
		?>
	  </select></td>
	<td>
		<br />
		<input type='hidden' name='stepPage' value='general2.html'>
		<input name="submit" type="submit" value="&nbsp View &nbsp" onClick="sgeneral(getFormData('generalSearch'));">
	</td>
  </tr>
</table>
</form>
<br />
<?

echo $res_table1;
echo '<br />';
echo $res_table2;
echo '<br />';
echo $res_table3;
echo '<br />';
echo $res_table4;
?>